const tinymceInit = (selector, callback, obj) => {
  if (typeof obj === "undefined") {
    obj = {};
  }
  let dir = obj.dir ? obj.dir : "";
  tinymce.init({
    selector: selector,
    height: obj.height || 525,
    branding: obj.branding || false,
    language: "zh_CN",
    plugins:
      "code print preview fullscreen paste searchreplace save autosave link autolink image imagetools media table codesample lists advlist hr charmap emoticons anchor directionality pagebreak quickbars nonbreaking visualblocks visualchars wordcount",
    toolbar:
      "fullscreen preview code | undo redo | forecolor backcolor | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | formatselect fontselect fontsizeselect | link image media emoticons charmap anchor pagebreak codesample | ltr rtl",
    toolbar_drawer: obj.toolbar_drawer || "sliding",
    file_picker_types: obj.file_picker_types || "file image",
    relative_urls: obj.relative_urls || false,
    remove_script_host: obj.remove_script_host || false,
    document_base_url: BASEURL,
    images_upload_handler: function (blobInfo, success, failure, progress) {
      var xhr, formData;
      var file = blobInfo.blob(); //转化为易于理解的file对象
      xhr = new XMLHttpRequest();
      xhr.withCredentials = false;
      xhr.open("POST", BASEURL + "common/file/upload");
      xhr.upload.onprogress = function (e) {
        progress((e.loaded / e.total) * 100);
      };
      xhr.onload = function () {
        var res = JSON.parse(xhr.responseText);
        if (res.code === 200) {
          success(res.data.url, { name: res.data.name });
        } else {
          failure(res.data.msg);
          parent.layer && parent.layer.msg(res.message, { icon: 2 });
          return false;
        }
      };
      formData = new FormData();
      formData.append("baseDir", obj.baseDir || "");
      formData.append("dir", dir || "image");
      formData.append("file", file, file.name); //此处与源文档不一样
      xhr.send(formData);
    },
    file_picker_callback: function (callback, value, meta) {
      //文件分类
      var filetype = ".pdf, .txt, .zip, .rar, .7z, .doc, .docx, .xls, .xlsx, .ppt, .pptx, .mp3, .mp4";
      //为不同插件指定文件类型
      switch (meta.filetype) {
        case "image":
          filetype = ".jpg, .jpeg, .png, .gif";
          dir += "/image";
          break;
        case "media":
          filetype = ".mp3, .mp4";
          dir += "/media";
          break;
        case "file":
          dir += "/file";
          break;
      }
      //模拟出一个input用于添加本地文件
      var input = document.createElement("input");
      input.setAttribute("type", "file");
      input.setAttribute("accept", filetype);
      input.click();
      input.onchange = function () {
        let file = this.files[0];
        let xhr, formData;
        xhr = new XMLHttpRequest();
        xhr.withCredentials = false;
        xhr.open("POST", BASEURL + "common/file/upload");
        xhr.onload = function () {
          var res = JSON.parse(xhr.responseText);
          if (res.code === 200) {
            try {
              res.data = $smUtil.decrypt(res.data);
            } catch (e) {}
            callback(res.data.url, { text: res.data.name, name: res.data.name });
          } else {
            parent.layer && parent.layer.msg(res.message, { icon: 2 });
            return false;
          }
        };
        formData = new FormData();
        formData.append("baseDir", obj.baseDir || "");
        formData.append("dir", dir || "");
        formData.append("file", file, file.name); //此处与源文档不一样
        xhr.send(formData);
      };
    },
    init_instance_callback: function (editor) {
      $base.removeLoading();
      callback(editor);
    },
  });
};
